﻿Public Class frmEditSection
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Public Primary_Code As Integer
    Dim on_load As Boolean = True
    Dim Str_OldName As String
    Dim Str_OldCode As String
    Dim Str_OldCate As String
#Region "Function"
    Sub Load_AutoComplete_SectionName(Optional ByVal TypeCode As String = "%", Optional ByVal KindCode As String = "%", Optional ByVal Attribute As String = "%")
        Try
            Strsql = "Select Section_Name from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "'"


            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "sections")
            If DT_Category.Rows.Count = 0 Then Exit Sub
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_Category.Rows.Count - 1
                ColItemName.Add(DT_Category.Rows(i).Item("Section_Name"))
            Next

            With TxtSectionName
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .AutoCompleteCustomSource = ColItemName

            End With


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_CategoriesName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Category()
        Try
            Strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Category"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try
    End Sub
    Function Validate_data() As Boolean
        Validate_data = False
        Try
            If Me.TxtSectionName.Text = "" Then
                MsgBox("กรุณากรอกข้อมูลให้ครบทุกช่อง", MsgBoxStyle.Critical, "ตรวจสอบข้อมูล")
                Exit Function
            End If

            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
#End Region

    Private Sub frmEditSection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Category()
        CboCategories.SelectedValue = frmSection.CboCategories.SelectedValue
        Str_OldName = Me.TxtSectionName.Text
        Str_OldCode = Me.TxtSectionCode.Text
        Str_OldCate = Me.CboCategories.SelectedValue
        Load_AutoComplete_SectionName()
    End Sub

    Private Sub GroupControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl2.Paint

    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged

    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try

            If Str_OldName <> Me.TxtSectionName.Text And Str_OldCode <> Me.TxtSectionCode.Text And Str_OldCate = Me.CboCategories.SelectedValue Then
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                Me.Close()
            End If

            If Validate_data() = False Then Exit Sub
            If MsgBox("คุณต้องการบันทึกการเปลี่ยนแปลงใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการแก้ไข") = MsgBoxResult.No Then Exit Sub

            Strsql = "Update sections set section_Name='" & Me.TxtSectionName.Text & "',Section_Code='" & Me.TxtSectionCode.Text & "' where Section_ID = " & Primary_Code

            If _mysql.MySQLExecute(Strsql) > 0 Then
                MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการปรับปรุงข้อมูล")
                Me.DialogResult = System.Windows.Forms.DialogResult.OK

            Else
                ' MsgBox("ขออภัยไม่สามารถบันทึกการเปลี่ยนแปลงข้อมูลได้", MsgBoxStyle.Critical, "ผลการเพิ่มข้อมูล")
                frmDebug.lblFormName.Text = Me.Name
                frmDebug.lblFunctionName.Text = "OK_Button_Click"
                frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                frmDebug.MemoSQL.Text = Strsql
                frmDebug.ShowDialog()
                Exit Sub
            End If



            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (6,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "OK_Button_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
            End Select
            Me.Dispose()
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Section"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
End Class